package com.uenpay.im.weight.relationlistview;

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.funds.FundsInDetailEntity;
import com.uenpay.im.ui.old.activity.funds.FundsInEntity;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ExpandListAdapter extends BaseExpandableListAdapter implements
		RelationListView.QHeaderAdapter {
	private TextView date_tv;
	private TextView amount_tv;
	private RelationListView listView;
	private Context context;
	private List<FundsInEntity> data;
	private HashMap<Integer, Integer> groupChildCount = new HashMap<Integer, Integer>();

	public ExpandListAdapter(Context context, RelationListView listView) {
		 this.context = context;
		this.context = context;
		this.data = new ArrayList<FundsInEntity>();
		this.listView = listView;
	}

	@Override
	public int getJyhHeaderState(int groupPosition, int childPosition) {
		final int childCount = getChildrenCount(groupPosition);
		if (childPosition == childCount - 1
				&& listView.isGroupExpanded(groupPosition)) {
			return PINNED_HEADER_GONE;
		} else if (childPosition == -1
				|| !listView.isGroupExpanded(groupPosition)) {
			return PINNED_HEADER_GONE;
		} else {
			return PINNED_HEADER_VISIBLE;
		}
	}

	@Override
	public void configureJyhHeader(final View header, final int groupPosition,
			int childPosition, int alpha) {
		date_tv = (TextView) header.findViewById(R.id.date_tv);
		amount_tv = (TextView) header.findViewById(R.id.amount_tv);
		if (!TextUtils.isEmpty(data.get(groupPosition).createtime)
				&& data.get(groupPosition).createtime.indexOf(" ") > -1) {
			String date[] = data.get(groupPosition).createtime.split(" ");
			if (groupPosition == data.size() -1) {
				date_tv.setText(MessageFormat.format(context.getResources()
						.getString(R.string.in_group_funds), date[0]));
			} else {
				date_tv.setText(MessageFormat.format(context.getResources()
						.getString(R.string.received_group_funds), date[0]));
			}
		}

		amount_tv.setText(MessageFormat.format(
				context.getResources().getString(R.string.red_size),
				data.get(groupPosition).amount));

	}

	private HashMap<Integer, Integer> groupStatusMap = new HashMap<Integer, Integer>();

	@Override
	public void setGroupClickStatus(int groupPosition, int status) {
		groupStatusMap.put(groupPosition, status);
	}

	@Override
	public int getGroupClickStatus(int groupPosition) {
		if (groupStatusMap.containsKey(groupPosition)) {
			return groupStatusMap.get(groupPosition);
		} else {
			return 0;
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return data.get(groupPosition).arrayList.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		FriendHolder holder;
		if (convertView == null) {
			holder = new FriendHolder();
			LayoutInflater inflater = (LayoutInflater) parent.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_funds_detail_layout,
					parent, false);
			holder.name = ((TextView) convertView.findViewById(R.id.nick_name));
			holder.date_tv = ((TextView) convertView.findViewById(R.id.date_tv));
			holder.amount = ((TextView) convertView
					.findViewById(R.id.amount_tv));
			holder.avatar = (RoundImageView) convertView
					.findViewById(R.id.avatar_iv);
			holder.last_line_view = convertView
					.findViewById(R.id.last_line_view);
			convertView.setTag(holder);
		} else {
			holder = (FriendHolder) convertView.getTag();
		}

		if ((childPosition == data.get(groupPosition).arrayList.size() - 1 && childPosition != 0)
				|| data.get(groupPosition).arrayList.size() == 1) {
			LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) holder.last_line_view
					.getLayoutParams();
			linearParams.leftMargin = 0;
			linearParams.rightMargin = 0;
			holder.last_line_view.setLayoutParams(linearParams);
		} else {
			LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) holder.last_line_view
					.getLayoutParams();
			linearParams.leftMargin = Utils.dip2px(context, 70);
			linearParams.rightMargin = Utils.dip2px(context, 10);
			holder.last_line_view.setLayoutParams(linearParams);
		}
		FundsInDetailEntity userInfo = data.get(groupPosition).arrayList
				.get(childPosition);

		if (userInfo.status == 1) {
			/** 已经缴费 **/
			holder.amount.setText(MessageFormat.format(context.getResources()
					.getString(R.string.red_size), userInfo.amount));
			holder.amount.setTextColor(context.getResources().getColor(
					R.color.the_color_black));
		} else {
			holder.amount.setText(R.string.not_pay);
			holder.amount.setTextColor(context.getResources().getColor(
					R.color.the_main_color_normal));
		}

		holder.name.setText(userInfo.nickname);
		holder.date_tv.setText(userInfo.updatetime);
		holder.avatar.SetUrl(APIConfiguration.getAvatarUrlNormal(
				userInfo.custid, PacketType.PERSONAL));
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (data.isEmpty()) {
			return 0;
		}

		return data.get(groupPosition).arrayList.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return data.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return data.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		GroupHolder groupHolder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) parent.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(
					R.layout.item_funds_group, parent, false);
			groupHolder = new GroupHolder();
			groupHolder.selected_iv = (ImageView) convertView
					.findViewById(R.id.selected_iv);
			groupHolder.date_tv = (TextView) convertView
					.findViewById(R.id.date_tv);
			groupHolder.amount_tv = (TextView) convertView
					.findViewById(R.id.amount_tv);
			convertView.setTag(groupHolder);

		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		
		groupHolder.amount_tv.setText(MessageFormat.format(context
				.getResources().getString(R.string.red_size), data
				.get(groupPosition).totalamount));
		
		if (!TextUtils.isEmpty(data.get(groupPosition).createtime)) {
			String date[] = data.get(groupPosition).createtime.split(" ");
			if (groupPosition == data.size() -1) {
				groupHolder.date_tv.setText(MessageFormat.format(context
						.getResources().getString(R.string.in_group_funds),
						date[0]));
			} else {
				groupHolder.date_tv.setText(MessageFormat.format(context
						.getResources()
						.getString(R.string.received_group_funds), date[0]));
			}
		}

		if (isExpanded) {
			groupHolder.selected_iv
					.setImageResource(R.drawable.icon_arrow_top_one);
		} else {
			groupHolder.selected_iv
					.setImageResource(R.drawable.icon_arrow_bottom);
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	class GroupHolder {
		private ImageView selected_iv;
		private TextView date_tv;
		private TextView amount_tv;
	}

	class FriendHolder {
		private TextView name;
		private TextView date_tv;
		private TextView amount;
		private RoundImageView avatar;
		private View last_line_view;
	}

	public void setData(List<FundsInEntity> data) {
		this.data = data;
		groupChildCount.clear();
		for (int i = 0; i < data.size(); i++) {
			groupChildCount.put(i, data.get(i).arrayList.size());
		}

		this.notifyDataSetChanged();
	}

	class getHeadGroupTask extends AsyncTask<String, String, String> {

		View view;

		public getHeadGroupTask(View headView) {
			view = headView;
		}

		@Override
		protected String doInBackground(String... params) {

			return null;
		}

		@Override
		protected void onProgressUpdate(String... values) {

			super.onProgressUpdate(values);
		}
	}

}